﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;

using ATSV.Web;
using ATSV.Web.Services;
using System.ServiceModel.DomainServices.Client;

namespace ATSV.Views
{
    public partial class AdminCockpit : ChildWindow
    {
        ATSVDataContext _context = new ATSVDataContext();

        public AdminCockpit()
        {
            InitializeComponent();
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void newsItemSetDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void speichernButton_Click(object sender, RoutedEventArgs e)
        {
            NewsItemSet newNews = new NewsItemSet();
            newNews.Titel = titelTextBox.Text;
            newNews.Body = bodyTextBox.Text;
            newNews.Link = linkTextBox.Text;
            newNews.CreateDate = DateTime.Now;

            _context.NewsItemSets.Add(newNews);
            _context.SubmitChanges(OnSubmitCompleted, null);
        }

        private void OnSubmitCompleted(SubmitOperation so)
        {
            if (so.HasError)
            {
                MessageBox.Show(string.Format("Speichern fehlgeschlagen: {0}", so.Error.Message));
                so.MarkErrorAsHandled();
            }
            CheckChanges();
        }

        private void CheckChanges()
        {
            bool hasChanges = _context.HasChanges;
            speichernButton.IsEnabled = hasChanges;
            verwerfenButton.IsEnabled = hasChanges;
        }

        private void verwerfenButton_Click(object sender, RoutedEventArgs e)
        {
            _context.RejectChanges();

            titelTextBox.Text = "";
            bodyTextBox.Text = "";
            linkTextBox.Text = "";
        }

        private void spielerSetDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void teamSetDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }
    }
}

